﻿Imports _1065081_1065104.DTO
Imports _1065081_1065104.BUS
Imports _1065081_1065104.Global_Class
Public Class themnhanvien
    Private _nvbus As New CBusinhanvien()
#Region " form moving"
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    Private Sub BorderlessWindowDrag(ByVal sender As Object, ByVal e As MouseEventArgs)
        ReleaseCapture()
        SendMessage(Me.Handle, &HA1, 2, 0&)
    End Sub
    Private Sub frmAddnhanvien_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        BorderlessWindowDrag(sender, e)
    End Sub

#End Region
#Region "Windows Events"
    Private Sub themnhanvien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        xoa.Enabled = True
        sua.Enabled = True
        luu.Enabled = True
    End Sub
#End Region
#Region "Windows Events"

    Private Sub them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles them.Click
        ReNew()
        manv.Text = _nvbus.Getmanv("AGBC", "Manv", "nhanvien", 10).Trim()

        tennv.Focus()

        luu.Enabled = True
        them.Enabled = False
        them.Enabled = False
    End Sub
#End Region

    Private Sub luu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles luu.Click
        Try
            If CheckDataInput() = True Then
                _nvbus.Insert(AddnhanvienValue())
                sua.Enabled = True
                xoa.Enabled = True
                luu.Enabled = False
                them.Enabled = True
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Loi xu ly !", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub

    Private Sub boqua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles boqua.Click
        ReNew()
    End Sub

    Private Sub sua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sua.Click
        Try
            If CheckDataInput() = True Then
                _nvbus.Update(AddnhanvienValue())
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Loi xu ly !", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub

    Private Sub xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xoa.Click
        Try
            If MessageBox.Show("Bạn có thật sự muốn xóa ?", "Xac nhan", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
                _nvbus.Delete(manv.Text.Trim())
                ReNew()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Loi xu ly !", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub

    Private Sub cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel.Click
        Me.Close()
    End Sub

#Region "Process"
    Private Function AddnhanvienValue() As Cinfonhanvien
        Dim nhanvien As New Cinfonhanvien()
        nhanvien.manv = manv.Text.Trim()
        nhanvien.tennv = tennv.Text.Trim()
        nhanvien.ngaysinh = DateTimePicker1.Value
        nhanvien.diachi = dienthoai.Text.Trim()
        nhanvien.sodienthoai = diachi.Text.Trim()
        nhanvien.ngaythamgia = DateTimePicker2.Value
        nhanvien.phongban = phongban.Text.Trim()

        Return nhanvien
    End Function
    Private Function CheckDataInput() As Boolean
        Dim strErr As String = String.Empty

        If manv.Text.Trim() = "" Then
            strErr = "- Mã nhan Vien."
        End If
        If tennv.Text.Trim() = "" Then
            strErr = strErr & vbLf & "- Tên nhan Vien."
        End If
        If dienthoai.Text.Trim() = "" Then
            strErr = strErr & vbLf & "- Số chứng minh nhân dân."
        End If
        If chucvu.Text.Trim() = "" Then
            strErr = strErr & vbLf & "- Địa chỉ nhan Vien."
        End If
        If DateTime.Now.Year - DateTimePicker1.Value.Year < 15 Then
            strErr = strErr & vbLf & "- Ngày sinh không hợp lệ."
        End If

        If strErr <> String.Empty Then
            MessageBox.Show("Bạn phải hoàn tất các thông tin sau :" & vbLf & strErr, "Canh bao !", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            Return False
        End If

        Return True
    End Function
    Private Sub ReNew()
        manv.Text = ""
        tennv.Text = ""
        DateTimePicker1.Value = DateTime.Now
        dienthoai.Text = ""
        diachi.Text = ""
        chucvu.Text = ""
        phongban.Text = ""

        them.Enabled = True
        luu.Enabled = True
        sua.Enabled = True
        xoa.Enabled = True
    End Sub

#End Region
End Class